﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data.Linq;
using System.Linq;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Web;
using System.Security.Permissions;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Web.Resources;

namespace Catalyst.Web.DynamicData
{
    /// <summary>
    /// Enables the LinqDataSource to accpet where predicates to create complex queries.
    /// </summary>
    [Category("Data")]
    [ToolboxBitmap(typeof(DynamicLinqDataSource))]
    public class DynamicLinqDataSource : LinqDataSource, IPredicateDynamicDataSource
    {
        private DynamicLinqDataSourceView _view;

        public DynamicLinqDataSource()
        {
            CreateView();
        }

        /// <summary>
        /// Overrides the default CreateView to return a DynamicLinqDataSourceView as opposed to a LinqDataSourceView
        /// Thank you David Ebbo, Scott Hunter, and Marcin Dobosz of the ASP.NET Dynamic Data team!
        /// </summary>
        /// <returns></returns>
        protected override LinqDataSourceView CreateView()
        {
            if(_view == null)
                _view = new DynamicLinqDataSourceView(this, "DefaultView", this.Context);
            return _view;
        }

        /// <summary>
        /// Gets a collection of parameters that are used to create the Where predicate.
        /// </summary>
        /// <returns>
        /// A collection that contains the parameters that are used to generate the where predicate.
        /// </returns>
        [Browsable(false), Category("Data"), PersistenceMode(PersistenceMode.InnerProperty), DefaultValue((string)null), MergableProperty(false)]
        public DynamicParameterCollection WherePredicateParameters
        {
            get
            {
                return _view.WherePredicateParameters;
            }
        }
    }
}
